package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.cust.CustPersonInfoConstants;
import com.gopay.common.constants.cust.UserInfoConstants;
import com.gopay.common.domain.UserInfo;
import com.gopay.common.domain.dc.DcUserCardInfo;
import com.gopay.common.user.dao.UserInfoQueryDAO;

/**
 * UserInfoQuery DAO实现
 * 
 * @CustCorpInfoQueryManager.java
 * @author liuxin
 * @2013-1-5 上午9:49:38 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("userInfoQueryDao")
public class UserInfoQueryDAOImpl extends BaseDAOImpl<UserInfo, Serializable> implements UserInfoQueryDAO {

    @Override
    public UserInfo getUserInfoByEmail(String email) {
        if(StringUtils.isBlank(email)) {
            return  null;
        }

        boolean isEmail = email.indexOf("@") != -1;
        String hql = "from UserInfo where email = :email  and userStat <> :userStat2";
        if(!isEmail){
            hql = "from UserInfo where operMobile = :email  and userStat <> :userStat2";
        }
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("email", email);      
        param.put("userStat2", UserInfoConstants.USER_STAT_CANCEL);
        List<UserInfo> userInfoList = this.findByHql(hql, param);
        if (userInfoList == null || userInfoList.size() == 0) {
            return null;
        }
        return userInfoList.get(0);
    }

    @Override
    public List<UserInfo> getByCustId(String custId) {
        //必须增加排序字段，这样保证第一个元素是管理员  edit by fanghw 20161207
        return findByHql(" from UserInfo t where t.custId = '" + custId + "' order by t.userId asc");
    }

    @Override
    public UserInfo getUserInfoByEmailAndOprId(String email, String oprId) {
        // TODO Auto-generated method stub
        DetachedCriteria criteria = DetachedCriteria.forClass(UserInfo.class);
        criteria.add(Restrictions.eq("email",email));
        if(StringUtils.isNotBlank(oprId)){
            criteria.add(Restrictions.eq("operId",oprId));
        }
       
        criteria.add(Restrictions.ne("userStat", UserInfoConstants.USER_STAT_CANCEL));
        List<UserInfo> list = this.findByCriteria(criteria);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
	public List<UserInfo> getPasswordLockedStatusUser() {
        return null;
//    	DetachedCriteria criteria = DetachedCriteria.forClass(UserInfo.class);
//    	criteria.add(Restrictions.eq("userStat", UserInfoConstants.));
//    	
//    	List<UserInfo> list = this.findByCriteria(criteria);
//    	return list;
    }

    @Override
	public UserInfo getCorpAdminUserInfo(String custId) {
        String hql = "from UserInfo where custId = :custId and operId = :operId";
        
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("custId", custId);
        param.put("operId", UserInfoConstants.CORP_ADMIN_CODE);
        List<UserInfo> userInfoList = this.findByHql(hql, param);
        if (userInfoList == null || userInfoList.size() == 0) {
            return null;
        }
        return userInfoList.get(0);
    }

	@Override
	public UserInfo getUserInfoByCustIDAndOprId(String custId, String oprId) {
        String hql = "from UserInfo where custId = :custId and operId = :operId";
        
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("custId", custId);
        param.put("operId", oprId);
        List<UserInfo> userInfoList = this.findByHql(hql, param);
        if (userInfoList == null || userInfoList.size() == 0) {
            return null;
        }
        return userInfoList.get(0);
	}

	/**
     * 
     * @param custId 客户编号
     * @return 动态口令状态
     */
	@Override
	public DcUserCardInfo getDynamicPassword(String custId) {
		String hql = "from DcUserCardInfo where custId = '" +custId + "'";
		@SuppressWarnings("unchecked")
		List<DcUserCardInfo> DcUserCardInfoList = hibernateTemplate.find(hql);
		if (DcUserCardInfoList == null || DcUserCardInfoList.size() == 0) {
            return null;
        }
		return DcUserCardInfoList.get(0);
	}
	/**
	 * 动态口令状态的更新操作
	 */
	@Override
	public int updateDcUserCardInfo(String status,String custId) {
		
		int nums = 0;
		String hql = "";
		if("1".equals(status)){
			hql = "update DcUserCardInfo set consoleLockStat = '"+status+"' where custId = '"+custId+"' ";			
		}
		if("0".equals(status)){
			hql = "update DcUserCardInfo set consoleLockStat = '"+status+"' , pwdLockStat = '"+status+"' , tokenErrorNum = '"+status+"' where custId = '"+custId+"' ";
		}
		
		nums = super.updateByHql(hql);
		return nums;
	}
	/**
	 * 往DC_USER_CARD_INFO表中插入一条记录
	 */
	@Override
	public void addDcUserCardInfoBean(String custId, String newConsoleLockStat) {
		// TODO Auto-generated method stub
		DcUserCardInfo bean = new DcUserCardInfo();
		bean.setCustId(custId);
		bean.setConsoleLockStat(newConsoleLockStat);
		bean.setCreateTs(new Date());
		bean.setPwdLockStat("0");
		hibernateTemplate.saveOrUpdate(bean);
		
	}

	@Override
	public UserInfo getBySSoId(String ssoId) {
		List<UserInfo> userInfoList = findByHql(" from UserInfo t where t.ssoUserId = '" + ssoId + "'");
		 if (userInfoList == null || userInfoList.size() == 0) {
	            return null;
	        }
        return userInfoList.get(0);
	}


	@Override
	public UserInfo getUserInfoByUserId(String userId) {
		  String hql = "from UserInfo where userId = :userId";
	        
	        Map<String, Object> param = new HashMap<String, Object>();
	        param.put("userId", userId);
	        List<UserInfo> userInfoList = this.findByHql(hql, param);
	        if (userInfoList == null || userInfoList.size() == 0) {
	            return null;
	        }
	        return userInfoList.get(0);
	}

	@Override
	public UserInfo getCorpAdminorPerUserInfo(String custId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserInfo.class);
		criteria.add(Restrictions.eq("custId",custId));
		criteria.add(Restrictions.ne("userStat", UserInfoConstants.USER_STAT_CANCEL));
		criteria.add(Restrictions.in("userType",new String[]{"10","20"}));
		List<UserInfo> list = this.findByCriteria(criteria);
		return list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public UserInfo getUserInfoByMobile(String mobile) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserInfo.class);
        criteria.add(Restrictions.eq("custMob",mobile));
        criteria.add(Restrictions.eq("custStat", CustPersonInfoConstants.CUST_STAT_WAIT_EMAIL_CERTIFY));
        criteria.addOrder(Order.desc("createdTs"));
        List<UserInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
	}
	@Override
	public void updateUserSource(String custId,String userSource){
		  String hql = "update  UserInfo set userSource=:userSource where custId = :custId";
	      Map<String, Object> param = new HashMap<String, Object>();
	      param.put("userSource", userSource);
	      param.put("custId", custId);
	      this.updateByHql(hql, param);
	}


}
